<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ArticleController
 *
 * @author Kamdem Kenmogne
 */
class ArticleController extends Controller{
    //put your code here
    
    protected $app;
    
    public function __construct($app) 
    {
        parent::__construct($app);
        $this->app = $app;
    }
    
    /**
     * get All Articles
     * url - /register
     * method - GET
     */
    public function getAllArticles() 
    {
        $response = array();
        // call user
        $result = $this->app->myDB->getAllArticles();

        if($result != NULL){
            $this->app->authen->echoResponse(201, $result);
        }
        else{
            $response["error"] = TRUE;
            $response["message"] = "The requested resource doesn't exists";
            $this->app->authen->echoResponse(401, $response);
        }
    }
    
    /**
     * Listing all articles of particual user
     * method GET
     * url /articles          
     */
    public function getArticles()
    {
        $this->app->authen->authenticate();
        $response = array();
        //global $user_id;
        $user_id = $this->app->user_id;
        // fetching all user articles
        $result = $this->app->myDB->getAllUserArticles($user_id);
//        echo $user_id;

        if($result !=null){
            //$response["error"] = false;
            $this->app->authen->echoResponse(200, $result);
        }else{
            $response["error"] = TRUE;
            $response["message"] = "The requested resource doesn't exists";
            $this->app->authen->echoResponse(401, $response); 
        }
    }
    
    /**
     * Listing single article of particual user
     * method GET
     * url /articles/:articleId
     * Will return 404 if the article doesn't belongs to user
     */
    public function getArticle($article_id)
    {
//        $this->app->authen->authenticate();
        //global $user_id;
//        $user_id = $this->app->user_id;        
        $response = array();
        // fetch article
        $result = $this->app->myDB->getArticle($article_id);

        if ($result != NULL) {
            $this->app->authen->echoResponse(200, $result);
        } else {
            $response["error"] = true;
            $response["message"] = "The requested resource doesn't exists";
            $this->app->authen->echoResponse(404, $response);
        }
    }
    
    /**
     * Listing single article of particual user
     * method GET
     * url /articles/:articleId
     * Will return 404 if the article doesn't belongs to user
     */
    public function getArticleByCat($categorie_id)
    {
//        $this->app->authen->authenticate();
        //global $user_id;
//        $user_id = $this->app->user_id;        
        $response = array();
        // fetch article
        $result = $this->app->myDB->getArticleByCat($categorie_id);

        if ($result != NULL) {
            $this->app->authen->echoResponse(200, $result);
        } else {
            $response["error"] = true;
            $response["message"] = "The requested resource doesn't exists";
            $this->app->authen->echoResponse(404, $response);
        }
    }
    
    /**
     * Creating new article in db
     * method POST
     * url - /article/
     */
    public function addArticle()
    {
        //call the fonction authentificate
        $this->app->authen->authenticate();
                // check for required params
        $this->app->authen->verifyRequiredParams(array('categorie_Id','name',
           'description'));
        
        if (isset($_FILES['image'])) {
            move_uploaded_file($_FILES['image']['tmp_name'], 'images/' . basename($_FILES['image']['name']));
            $Titelbild = 'images/' . basename($_FILES['image']['name']);
        } else {
            $Titelbild = "images/bbcg.jpg";
        }
        
        $response = array();
        
        $categorie_Id = $this->app->request()->post('categorie_Id');
        $name = $this->app->request()->post('name');
        $description = $this->app->request()->post('description');
        $image = $this->app->request()->post('image', $Titelbild);
//        global $user_id;
        $user_id = $this->app->user_id;
        // creating new article
        $article_id = $this->app->myDB->createArticle($user_id, $categorie_Id, $name, 
            $description, $image);

        if ($article_id != NULL) {
            $response["error"] = false;
            $response["message"] = "Article created successfully";
            $response["articleId"] = $article_id;
            $this->app->authen->echoResponse(201, $response);
        } else {
            $response["error"] = true;
            $response["message"] = "Failed to create article. Please try again";
            $this->app->authen->echoResponse(200, $response);
        }
    }
    
    /**
     * Updating existing article
     * method PUT
     * url - /article/:id
     */
    public function editArticle($article_id)
    {
        //call the fonction authentificate
        $this->app->authen->authenticate();
       // check for required params
        $this->app->authen->verifyRequiredParams(array('categorie_Id','name',
           'description'));
        
        if (isset($_FILES['image'])) {
            move_uploaded_file($_FILES['image']['tmp_name'], 'images/' . basename($_FILES['image']['name']));
            $Titelbild = 'images/' . basename($_FILES['image']['name']);
        } else {
            $Titelbild = "";
        }
       
        //$article_id = $this->app->request()->put('articleId');
        $categorie_Id = $this->app->request()->put('categorie_Id');
        $name = $this->app->request()->put('name');
        $description = $this->app->request()->put('description');
        $image = $this->app->request()->put('image',$Titelbild);
//        global $user_id;
        $user_id = $this->app->user_id;
        // updating article
       
        $result = $this->app->myDB->updateArticle($user_id, $article_id, 
                $categorie_Id, $name, $description, $image);
         
        $response = array();
        
        if ($result) {
            // article updated successfully
            $response["error"] = false;
            $response["message"] = "article updated successfully";
        } else {
            // article failed to update
            $response["error"] = true;
            $response["message"] = "article failed to update. Please try again!";
        }
        $this->app->authen->echoResponse(200, $response); 
    }
    
    /**
     * Deleting article. Users can delete only their articles
     * method DELETE
     * url /articles
     */
    public function deleteArticle($article_id)
    {
        //call the fonction authentificate
        $this->app->authen->authenticate();
        
        $user_id = $this->app->user_id;
        $response = array();
        $result = $this->app->myDB->deleteArticle($user_id, $article_id);
        if ($result) {
            // article deleted successfully
            $response["error"] = false;
            $response["message"] = "Article deleted succesfully";
        } else {
            // article failed to delete
            $response["error"] = true;
            $response["message"] = "Article failed to delete. Please try again!";
        }
        $this->app->authen->echoResponse(200, $response);
    }
       
}
